//
//  YHMachinePullDownProductMenuView.m
//  YHApp
//
//  Created by -- on 2021/7/25.
//

#import "YHMachinePullDownProductMenuView.h"

@interface YHMachinePullDownProductMenuView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

/***/
@property (nonatomic, strong) HACanalItemMo *selmo;

@end

@implementation YHMachinePullDownProductMenuView

- (instancetype)init
{
    self = [super init];
    if (self) {

        [self layoutViews];
        
        MJRefreshGifHeader *mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        self.tableView.mj_header = mj_header;
        
        [self.tableView.mj_header beginRefreshing];

    }
    return self;
}

#pragma mark - 点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [super didTapBackgroundView:sender];
}

-(void)cancelClick {
    [self dismiss];
}

-(void)sureClick {
    if (self.selmo) {
        if (self.YHMachinePullDownProductMenuSureBlock) {
            self.YHMachinePullDownProductMenuSureBlock(self.selmo);
        }
        [self dismiss];
    }else {
        [MBProgressHUD showError:@"请选择产品"];
    }
}

-(void)show {
    [super show];
}
-(void)dismiss {
    [super dismiss];
}


-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    //顶部视图
    CGFloat alertViewH = kUIScreen_h*0.65+20;
    CGFloat alertViewW = kUIScreen_w;
    CGFloat alertViewX = 0;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY+20, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 15;
    self.alertView.layer.masksToBounds = YES;

    UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#111A34" text:@"选择产品" alignment:1];
    [self.alertView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *cancelButton = [[UIButton alloc]init];
    [self.alertView addSubview:cancelButton];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#666f8c"] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
    }];

    UIButton *sureButton = [[UIButton alloc]init];
    [self.alertView addSubview:sureButton];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor colorWithHexString:@"#666f8c"] forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [sureButton addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
    }];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titLabel.mas_bottom);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH);
    }];
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *myCcell = [tableView dequeueReusableCellWithIdentifier:@"HAChooseTabPopViewCellID"];
    if (!myCcell) {
        myCcell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HAChooseTabPopViewCellID"];
    }
    myCcell.selectionStyle = UITableViewCellSelectionStyleNone;
    BOOL isline = YES;
    if (indexPath.row == self.dataArray.count-1) {
        isline = NO;
    }
    [self pp_setCellWith:myCcell.contentView model:self.dataArray[indexPath.row] isLine:isline];
    return myCcell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (HACanalItemMo *model in self.dataArray) {
        model.isSel = NO;
    }
    
    HACanalItemMo *mo = self.dataArray[indexPath.row];
    mo.isSel = YES;
    self.selmo = mo;
    [self.tableView reloadData];
}

-(void)pp_setCellWith:(UIView *)contentView model:(HACanalItemMo *)model isLine:(BOOL)isLine{
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIImageView *selImgV = [UIImageView ha_createImageView];
    if (model.isSel) {
        selImgV.image = [UIImage imageNamed:@"ha_1_sel"];
    }else {
        selImgV.image = [UIImage imageNamed:@"ha_1_def"];
    }
    [contentView addSubview:selImgV];
    [selImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(0);
        make.width.mas_offset(18);
        make.height.mas_offset(18);
    }];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#111a34" text:model.text alignment:0];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(selImgV.mas_left).offset(-10);
        make.centerY.mas_equalTo(0);
    }];

    if (isLine) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];
    }
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.alertView addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(void)loadNewData {
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"product/items"];
    request1.requestMethod = SYRequestMethodGET;
    request1.parms = @{@"isAll":@"1"};

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_header endRefreshing];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }

            if (dataModel.code == 1) {
                [self.dataArray removeAllObjects];
                NSArray *array = [HACanalItemMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                [self.dataArray addObjectsFromArray:array];
                [self.tableView reloadData];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}


@end
